<?php

/**
 * AmplifyCMS
 *
 * This file is part of AmplifyCMS.
 *
 * AmplifyCMS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * AmplifyCMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

require_once('users_amp_controller.php');

/**
 * 
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 *
 */
class CapsController extends UsersAmpController
{

  /**
   *
   */
  protected function action_index()
  {
    parent::action_index();
    $this->response->set('caps', 
        $this->db->from('Cap')->orderBy($this->response->get('indexOrderBy'))->offset($this->response->get('indexOffset'))->limit($this->response->get('indexLimit'))->find());
  }

  /**
   *
   */
  protected function action_view($id = null)
  {
    try {
      $this->validateId($id);
      $this->response->set('cap', $this->db->load('Cap', $id));
    }
    
    catch (ParameterException $e) {
      $this->response->warning($e->getMessage());
      return $this->response->redirect('/users/caps');
    }
    
    catch (NotFoundException $e) {
      $this->response->warning(__('Capability not found'));
      return $this->response->redirect('/users/caps');
    }
  }

  /**
   *
   */
  protected function action_create()
  {
    return $this->response->redirectIf($this->form() !== false, '/users/caps');
  }

  /**
   *
   */
  protected function action_edit($id = null)
  {
    try {
      $this->validateId($id);
      return $this->response->redirectIf($this->form($id) !== false, '/users/caps');
    }
    
    catch (ParameterException $e) {
      $this->response->warning($e->getMessage());
      return $this->response->redirect('/users/caps');
    }
    
    catch (NotFoundException $e) {
      $this->response->warning(__('Capability not found'));
      return $this->response->redirect('/users/caps');
    }
  }

  /**
   *
   */
  protected function action_delete($id = null)
  {
    try {
      $this->validateId($id);
      $cap = $this->db->load('Cap', $id);
      $this->db->delete($cap);
      $this->response->notice(__('Capability deleted'));
    }
    
    catch (ParameterException $e) {
      $this->response->warning($e->getMessage());
    }
    
    catch (NotFoundException $e) {
      $this->response->warning(__('Capability not found'));
    }
    
    return $this->response->redirect('/users/caps');
  }

  /**
   *
   */
  private function form($id = null)
  {
    if (empty($id)) {
      $cap = new Cap();
      $this->response->set('formTitle', __('Create Capability'));
    }
    else {
      $cap = $this->db->load('Cap', $id);
      $this->response->set('formTitle', __('Edit Capability'));
    }
    
    $this->response->set('cap', $cap);
    
    if ($this->form) {
      try {
        $cap->copyAll($this->form);
        $cap->validateAll();
        $this->db->save($cap);
        $this->response->notice(__('Data saved'));
        
        return $cap;
      }
      
      catch (EmptyException $e) {
        $this->response->notice(__('Nothing to save'));
      }
      
      catch (ValidationException $e) {
        $this->response->warning($cap->getErrors());
      }
    }
    
    $this->setTemplate('caps_form');
    
    return false;
  }
}

?>